package com.zhl.controller.article;

import com.zhl.controller.BaseServlet;
import com.zhl.model.Article;
import com.zhl.model.User;
import com.zhl.service.ArticleService;
import com.zhl.service.UserService;
import com.zhl.service.impl.ArticleServiceImpl;
import com.zhl.service.impl.UserServiceImpl;
import com.zhl.utils.page.PageEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {

    ArticleService articleService = new ArticleServiceImpl();
    UserService userService = new UserServiceImpl();

    public void findArticles(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        try {
            Map<String, String> param = getParam(req);
            Integer currPage;
            if (null == param.get("currPage") || "".equals(param.get("currPage"))) {
                currPage = 1;
            } else {
                currPage = Integer.parseInt(param.get("currPage"));
            }
            PageEntity<Article> articles = articleService.findArticles(title, currPage);
            req.getSession().setAttribute("articles",articles);
            req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/html/article_add.jsp").forward(req,resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        User autoId = (User)req.getSession().getAttribute("userById");
        boolean b = articleService.addArticle(title, content, autoId.getId(), autoId.getRealName());
        if (b){
            //发布成功，返回0
            resp.getWriter().write("0");
        }
        //发布失败，返回1
        else {
            resp.getWriter().write("1");
        }
    }

    public void articleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String articleId = req.getParameter("articleId");

        //获取当前id文章的信息
        Map<String, Object> articleById = articleService.findArticleById(Integer.parseInt(articleId));
        req.getSession().setAttribute("articleById",articleById);

        //对当前文章进行收藏处理
        User user = (User)req.getSession().getAttribute("userById");
        List<Integer> collectById = userService.findUserCollectById(user.getId());
        boolean b = collectById.contains(Integer.parseInt(articleId));
        req.getSession().setAttribute("b",b);

        //查找当前用户关注该文章的伙伴信息
        List<User> focusFavoriteById = userService.findFocusFavoriteById(user.getId(), Integer.parseInt(articleId));
        req.getSession().setAttribute("focusFavoriteById",focusFavoriteById);

        //转发到jsp页面
        req.getRequestDispatcher("/html/article_detail.jsp").forward(req,resp);
    }

    //更新收藏
    public void updateCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String artId = req.getParameter("artId");
        String collect = req.getParameter("collect");
        User user = (User)req.getSession().getAttribute("userById");
        //返回a,1:取消收藏，0：收藏成功
        Integer a = articleService.updateCollect(user.getId(), Integer.parseInt(artId), collect);
        resp.getWriter().write(String.valueOf(a));
    }

    //查找当前用户收藏文章
    public void findArticleByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        User user = (User)req.getSession().getAttribute("userById");
        List<Map<String, Object>> articleByUid = articleService.findArticleByUid(user.getId(),title);
        req.getSession().setAttribute("articleByUid",articleByUid);
        req.getRequestDispatcher("/html/article_collect.jsp").forward(req,resp);
    }

}
